
;some 16 bit and fixed point stuff
f2d:
	tsx
	cmp   #$80
	ror   $101,x
	ror   a
	cmp   #$80
	ror   $101,x
	ror   a
	cmp   #$80
	ror   $101,x
	ror   a
	cmp   #$80
	ror   $101,x
	ror   a
	vmnext

f2s:
	tsx
	asl	a
	rol	$101,x
	asl	a
	rol	$101,x
	asl	a
	rol	$101,x
	asl	a
	rol	$101,x
	pla
	vmnext


; following this in bytestream is 2 zp addresses.  O is added to get the final addresses.
dfetcho:
	pha
	ldy VM_O
	lda #0
	sta VM_B+1
	ifetch
	sta VM_B
	lda (VM_B),y
	pha
	ifetch
	sta VM_B
	lda (VM_B),y
	ldy VM_IP
	vmnext


; this is a far better way to do it.
xfetch:
	pha
	ldx VM_O
	lda OBJXhigh,x
	pha
	lda OBJXlow,x
	vmnext

yfetch:
	pha
	ldx VM_O
	lda OBJYhigh,x
	pha
	lda OBJYlow,x
	vmnext

;todo: subtract camera pos
screenxy:
	pha
	ldx VM_O
	lda OBJXhigh,x
	pha
	lda OBJXlow,x
	tsx
	asl	a
	rol	$101,x
	asl	a
	rol	$101,x
	asl	a
	rol	$101,x
	asl	a
	rol	$101,x
	ldx VM_O
	lda OBJYhigh,x
	pha
	lda OBJYlow,x
	jmp f2s

vxfetch:
	pha
	ldx VM_O
	lda OBJVXhigh,x
	pha
	lda OBJVXlow,x
	vmnext

vyfetch:
	pha
	ldx VM_O
	lda OBJVYhigh,x
	pha
	lda OBJVYlow,x
	vmnext

xstore:
	ldx VM_O
	sta OBJXlow,x
	pla
	sta OBJXhigh,x
	vmnext_pla

ystore:
	ldx VM_O
	sta OBJYlow,x
	pla
	sta OBJYhigh,x
	vmnext_pla

vxstore:
	ldx VM_O
	sta OBJVXlow,x
	pla
	sta OBJVXhigh,x
	vmnext_pla

vystore:
	ldx VM_O
	sta OBJVYlow,x
	pla
	sta OBJVYhigh,x
	vmnext_pla


updpal:  ; color#  A=src (A is not incremented)
	tax
	sty VM_TEMP
	ldy #0
	lda (VM_A),y
	sta PAL_BUF,x
	iny
	inx
	lda (VM_A),y
	sta PAL_BUF,x
	iny
	inx
	lda (VM_A),y
	sta PAL_BUF,x
	ldy VM_TEMP
	vmnext_pla



everyother:
	pha
	lda FRAMECNT
	and #1
	vmnext




padstate:
	tax
	lda <PAD_STATE,x
	vmnext

padrelease:
	tax
	lda <PAD_STATEL,x
	eor <PAD_STATE,x
	and <PAD_STATE,x
	vmnext

padpress:
	tax
	lda <PAD_STATE,x
	eor <PAD_STATEL,x
	and <PAD_STATE,x
	vmnext


set_ppu_mask:
   sta PPU_MASK_VAR
   pla
   vmnext

ppuaddress: ; addrh/l --
	tax
	pla
	sta PPU_ADDR
	stx PPU_ADDR
	vmnext_pla

ppustore: ; n --
	sta PPU_DATA
	vmnext_pla

forceblank:
	ldx #0
	stx PPU_MASK
	inx
	stx FORCED_BLANK
	vmnext

banktable:              ; Write to this table to switch banks.
  .byte $00, $01, $02, $03, $04, $05, $06
;  .byte $07, $08, $09, $0A, $0B, $0C, $0D, $0E
  ; UNROM needs only the first line of this table (0-6)
  ; for UOROM use both lines (0-14).

bankswitch:   ; newbank --
   tax
   stx CURRENT_BANK      ; save the current bank in RAM so the NMI handler can restore it
	ldx CURRENT_BANK
   lda banktable, x      ; read a byte from the banktable
   sta banktable, x      ; and write it back, switching banks
	vmnext_pla

